﻿//Begin CharacterCreation Functions
var img = new Image;
var img2 = new Image;

var Char_Class = 'f';
var Char_Race = 'b';
var Char_Gender = 'm';


var RaceImage_M_B = {
    sX: 0,
    sY: 470,
    sW: 237,
    sH: 235,
    dX: 397,
    dY: 3,
    dW: 237,
    dH: 235
};
var RaceImage_F_B = {
    sX: 0,
    sY: 0,
    sW: 237,
    sH: 235,
    dX: 397,
    dY: 3,
    dW: 237,
    dH: 235
};

function CharacterCreation() {

}

CharacterCreation.Initialize = function () {
    this.context = document.getElementById("CharacterCreationCanvas").getContext("2d");
}

CharacterCreation.Draw = function () {
    this.context.drawImage(img, 0, 0, 640, 480, 0, 0, 640, 480); //background
    this.context.drawImage(img2, RaceImage_M_B.sX, RaceImage_M_B.sY, RaceImage_M_B.sW, RaceImage_M_B.sH, RaceImage_M_B.dX, RaceImage_M_B.dY, RaceImage_M_B.dW, RaceImage_M_B.dH); //img, sX, sY, sW, sH, dX, dY, dW, dH
}

CharacterCreation.Start = function () {
    //imgLoaded = false;
    //img2Loaded = false;

    //img.onload = function () {
    //    imgLoaded = true
    //    img2.onload = function () {
    //        img2Loaded = true;
    //            CharacterCreation.Initialize();
    //            CharacterCreation.Draw();
    //    }
    //}

    var imgLoaded = false;
    var img2Loaded = false
    img.onload = function () {
        imgLoaded = true;
        if (img2Loaded == true) {
            CharacterCreation.Initialize();
            CharacterCreation.Draw();
        }
    }
    img2.onload = function () {
        img2Loaded = true;
        if (imgLoaded == true) {
            CharacterCreation.Initialize();
            CharacterCreation.Draw();
        }
    }

    var imgurl = window.location.protocol + '//' + window.location.host + '/Images/SpriteSheets/006.bmp';
    img.src = imgurl;
    var imgurl2 = window.location.protocol + '//' + window.location.host + '/Images/SpriteSheets/005.png';
    img2.src = imgurl2;

}

function SetGender(gender) {
    this.context = document.getElementById("CharacterCreationCanvas").getContext("2d");
    switch (gender) {
        case 'm':
            Char_Gender = 'm';
            $('#btnMale').attr('src', 'Images/Buttons/maleBtnDown.png');
            $('#btnFemale').attr('src', 'Images/Buttons/femaleBtnUp.png');

            //switch depending on race
            $('#imgClass').attr('src', 'Images/crits/alive/113.bmp');
            this.context.drawImage(img2, RaceImage_M_B.sX, RaceImage_M_B.sY, RaceImage_M_B.sW, RaceImage_M_B.sH, RaceImage_M_B.dX, RaceImage_M_B.dY, RaceImage_M_B.dW, RaceImage_M_B.dH);
            break;
        case 'f':
            Char_Gender = 'f';
            $('#btnMale').attr('src', 'Images/Buttons/maleBtnUp.png');
            $('#btnFemale').attr('src', 'Images/Buttons/femaleBtnDown.png');

            //switch depending on race
            $('#imgClass').attr('src', 'Images/crits/alive/122.bmp');
            this.context.drawImage(img2, RaceImage_F_B.sX, RaceImage_F_B.sY, RaceImage_F_B.sW, RaceImage_F_B.sH, RaceImage_F_B.dX, RaceImage_F_B.dY, RaceImage_F_B.dW, RaceImage_F_B.dH);
            break;
    }

}

function SetStat(stat, dir) {

    switch (stat) {
        case 'S':
            if (dir == 'U') { $("#Strength").val(parseInt($("#Strength").val()) + 1); } else { $("#Strength").val(parseInt($("#Strength").val()) - 1); }
            break;

    }
}

function SetRace(race) {

}
//End CharacterCreation Functions


//Begin Service Calls
function CreateCharacter() {
    
    var varType = "POST";
    var varUrl = "services/CharacterService.svc/Create";
    var varData = '{"UserName": "' + $('#UserName').val() + '", "Password": "' + $('#Password').val() + '"}';
    var varContentType = "application/json; charset=utf-8";
    var varDataType = "json";
    var varProcessData = true;

    $.ajax({
        type: varType, //GET or POST or PUT or DELETE verb
        url: varUrl, // Location of the service
        data: varData, //Data sent to server
        contentType: varContentType, // content type sent to server
        dataType: varDataType, //Expected data format from server
        processdata: varProcessData, //True or False
        success: function (msg) {//On Successfull service call
            ServiceSucceeded(msg);
        },
        error: ServiceFailed// When Service call fails
    });
}

function CancelCharacterCreation() {
    alert('Not Implemented');
}

function ServiceSucceeded(result) {//When service call is sucessful
    if (result.LoginResult.ID > 0) {
        window.location = 'ConferenceRoom.aspx?ID=' + result.LoginResult.GUID;
    } else {
        var ErrMsgs = 'Error Logging in: \n';
        for (i = 0; i < result.LoginResult.ErrorMessages.length; i++) {
            ErrMsgs += ' ' + result.LoginResult.ErrorMessages[i] + ' \n';
        }

        alert(ErrMsgs);
    }
}
function ServiceFailed(result) {
    alert('Login Service call failed: ' + result.status + '' + result.statusText);
}